package com.aizhixin.lab.project.template.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.project.template.domain.ModuleTaskFileDomain;
import com.aizhixin.lab.project.template.domain.WorkOrderFileDomain;
import com.aizhixin.lab.project.template.entity.ModuleTaskFile;
import com.aizhixin.lab.project.template.entity.Project;
import com.aizhixin.lab.project.template.entity.WorkOrderFile;
import com.aizhixin.lab.project.template.repository.ModuleTaskFileRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class ModuleTaskFileService {
    @Autowired
    private ModuleTaskFileRepository taskFileRepository;
    public ModuleTaskFile save(ModuleTaskFile moduleTaskFile) {
        if (null == moduleTaskFile.getId() || "".equals(moduleTaskFile.getId())) {
            moduleTaskFile.setId(UUID.randomUUID().toString());
        }
        return taskFileRepository.save(moduleTaskFile);
    }
    public String save(List<ModuleTaskFile> moduleTaskFile) {
        taskFileRepository.save(moduleTaskFile);
        return "OK";
    }
    public List<ModuleTaskFile> findTaskFile(String taskId){
       return taskFileRepository.findByTaskIdAndDeleteFlag(taskId, DataValidity.VALID.getState());
    }
    public Integer deleteByTaskId(String taskId){
     return taskFileRepository.deleteByTaskId(taskId);
    }
    public Map<String, Object> delete(String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        ModuleTaskFile moduleTaskFile = taskFileRepository.findOne(id);
        if (!Objects.isNull(moduleTaskFile)) {
            moduleTaskFile.setDeleteFlag(DataValidity.INVALID.getState());
            taskFileRepository.save(moduleTaskFile);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, "删除成功");
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "未找到文件信息");
        }
        return result;
    }
    public ModuleTaskFile save(ModuleTaskFileDomain domain){
        ModuleTaskFile moduleTaskFile = new ModuleTaskFile();
        BeanUtils.copyProperties(domain,moduleTaskFile);
        moduleTaskFile.setId(UUID.randomUUID().toString());
        moduleTaskFile.setDeleteFlag(DataValidity.VALID.getState());
        return taskFileRepository.save(moduleTaskFile);
    }
}
